package cn.edu.sgu.www.mhxysy.service.chongwu_equipment.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.dto.chongwu_equipment.ChongwuTaozhuangSkillCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu_equipment.ChongwuTaozhuangSkillCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_equipment.ChongwuTaozhuangSkillCategoryMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.chongwu_equipment.ChongwuTaozhaungSkillCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuTaozhaungSkillCategoryServiceImpl implements ChongwuTaozhaungSkillCategoryService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final ChongwuTaozhuangSkillCategoryMapper chongwuTaozhuangSkillCategoryMapper;

	@Autowired
	public ChongwuTaozhaungSkillCategoryServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			ChongwuTaozhuangSkillCategoryMapper chongwuTaozhuangSkillCategoryMapper) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.chongwuTaozhuangSkillCategoryMapper = chongwuTaozhuangSkillCategoryMapper;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_CWTZ;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(ChongwuTaozhuangSkillCategoryDTO chongwuTaozhuangSkillCategoryDTO) {
		chongwuTaozhuangSkillCategoryDTO.setId(null);

		ChongwuTaozhuangSkillCategory chongwuTaozhuangSkillCategory = toEntity(chongwuTaozhuangSkillCategoryDTO);

		chongwuTaozhuangSkillCategoryMapper.insert(chongwuTaozhuangSkillCategory);
	}

	@Override
	public void updateById(ChongwuTaozhuangSkillCategoryDTO chongwuTaozhuangSkillCategoryDTO) {
		if (chongwuTaozhuangSkillCategoryDTO.getId() == null) {
			throw new GlobalException(ResponseCode.BAD_REQUEST, "套装技能类型ID不能为空！");
		}

		ChongwuTaozhuangSkillCategory chongwuTaozhuangSkillCategory = toEntity(chongwuTaozhuangSkillCategoryDTO);

		chongwuTaozhuangSkillCategoryMapper.updateById(chongwuTaozhuangSkillCategory);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<ChongwuTaozhuangSkillCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				chongwuTaozhuangSkillCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public List<ChongwuTaozhuangSkillCategory> selectAll() {
		return chongwuTaozhuangSkillCategoryMapper.selectList(null);
	}

	@Override
	public ChongwuTaozhuangSkillCategory selectById(Integer id) {
		return chongwuTaozhuangSkillCategoryMapper.selectById(id);
	}

	@Override
	public ChongwuTaozhuangSkillCategory selectByChongwuId(Integer chongwuId) {
		QueryWrapper<ChongwuTaozhuangSkillCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("chongwu_id", chongwuId);

		return chongwuTaozhuangSkillCategoryMapper.selectOne(queryWrapper);
	}

	@Override
	public Page<ChongwuTaozhuangSkillCategory> selectByPage(Pager<ChongwuTaozhuangSkillCategory> pager) {
		Page<ChongwuTaozhuangSkillCategory> page = Pager.ofPage(pager);

		return chongwuTaozhuangSkillCategoryMapper.selectPage(page, null);
	}

	private ChongwuTaozhuangSkillCategory toEntity(ChongwuTaozhuangSkillCategoryDTO chongwuTaozhuangSkillCategoryDTO) {
		Integer id = chongwuTaozhuangSkillCategoryDTO.getId();
		String name = chongwuTaozhuangSkillCategoryDTO.getName();

		// 验证技能名称是否重复
		QueryWrapper<ChongwuTaozhuangSkillCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.ne(id != null, "id", id);
		queryWrapper.eq("name", name);

		Long count = chongwuTaozhuangSkillCategoryMapper.selectCount(queryWrapper);

		if (count > 0) {
			throw new GlobalException(ResponseCode.CONFLICT, "套装名称重复！");
		}

		ChongwuTaozhuangSkillCategory chongwuTaozhuangSkillCategory = new ChongwuTaozhuangSkillCategory();

		chongwuTaozhuangSkillCategory.setId(id);
		chongwuTaozhuangSkillCategory.setName(name);
		chongwuTaozhuangSkillCategory.setNote(chongwuTaozhuangSkillCategoryDTO.getNote());
		chongwuTaozhuangSkillCategory.setChongwuId(chongwuTaozhuangSkillCategoryDTO.getChongwuId());

		return chongwuTaozhuangSkillCategory;
	}

}